/**
 * 用户功能模块
 * 1.用户登录校验,登录成功，签发token
 */
const db = require("../dbUtil/index");
const sql = require("../dbUtil/sql");
const token = require("../util/token");

const functionCtl = require("./function/function");

module.exports = {
    //登录
    login: (req, res) => {
        //获取请求对象中的用户数据
        let { userName, password } = req.body;
        //调用数据库操作
        db.query(sql.users.login, [userName, userName, password], (err, results) => {
            if (err) {
                //返回一个json对象：状态码、状态描述、数据
                res.send({
                    code: 505,
                    msg: "数据库执行错误！",
                    data: {}
                });
            } else {
                //判断登录是否成功
                if (results.length > 0) {
                    //用户登录成功，签发token
                    token.setToken(results[0].userName, results[0].uid).then((token) => {
                        return res.send({
                            code: 200,
                            msg: "登陆成功",
                            data: { token: token, user: results[0] }
                        });
                    })
                } else {
                    res.send({
                        code: 201,
                        msg: "用户名或密码错误，登录失败",
                        data: {}
                    });
                }
            }
        })
    },
    //管理员登录
    adminLogin: (req, res) => {
        //获取请求对象中的用户数据
        let { userName, password } = req.body;
        if (userName == "admin" && password == "123123") {
            db.query(sql.users.adminLogin, [], (err, results) => {
                if (err) {
                    //返回一个json对象：状态码、状态描述、数据
                    res.send({
                        code: 505,
                        msg: "数据库执行错误！",
                        data: {}
                    });
                } else {
                    //管理员登录成功，签发token
                    token.setToken(results[0].userName, results[0].uid).then((token) => {
                        return res.send({
                            code: 200,
                            msg: "登陆成功",
                            data: { token: token, user: results[0] }
                        });
                    })
                }
            })
        } else {
            res.send({
                code: 201,
                msg: "用户名或密码错误，登录失败",
                data: {}
            });
        }
    },
    //注册
    regist: (req, res) => {
        //获取请求对象中的用户数据
        let { userName, account, password } = req.body;
        //调用数据库操作
        db.query(sql.users.register1, [userName, account], (err1, results1) => {
            if (err1) {
                //返回一个json对象：状态码、状态描述、数据
                res.send({
                    code: 505,
                    msg: "数据库1执行错误！",
                    data: {}
                });
            } else {
                //判断注册是否重复
                if (results1.length > 0) {
                    //用户注册失败
                    return res.send({
                        code: 202,
                        msg: "注册失败，用户名或账号重复",
                        data: {}
                    })
                } else {
                    db.query(sql.users.register2, [userName, account, password], (err2, results2) => {
                        if (err2) {
                            //返回一个json对象：状态码、状态描述、数据
                            res.send({
                                code: 505,
                                msg: "数据库2执行错误！",
                                data: {}
                            });
                        } else {
                            if (results2.affectedRows > 0) {//受影响行数
                                return res.send({
                                    code: 200,
                                    msg: "注册成功",
                                    data: {}
                                });
                            } else {
                                res.send({
                                    code: 201,
                                    msg: "注册失败",
                                    data: {}
                                });
                            }
                        }
                    })
                }
            }
        })
    },
    //全部用户数据
    usersAll: (req, res) => {
        functionCtl.all(sql.admin.usersAll, res);
    },
    //全部歌曲数据
    songsAll: (req, res) => {
        functionCtl.all(sql.admin.songsAll, res);
    },
    //全部歌单数据
    songListsAll: (req, res) => {
        db.query(sql.admin.songListsAll, [], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作错误！",
                    data: {}
                })
            } else {
                var songListsAll = [];
                for (var i in results) {
                    db.query(sql.admin.songListsAll2, [results[i].songListName], (err2, results2) => {
                        if (err2) {
                            res.send({
                                code: 505,
                                msg: "数据库操作错误！",
                                data: {},
                            })
                        } else {
                            songListsAll.push(results2[0]);
                            if (songListsAll.length == results.length) {
                                res.send({
                                    code: 200,
                                    msg: "数据库操作成功！",
                                    data: { data: songListsAll }
                                })
                            }
                        }
                    })
                }
            }
        })
    },
    //删除用户
    usersDelete: (req, res) => {
        let { userID } = req.query;
        db.query(sql.admin.usersDelete2, [userID], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作失败!",
                    data: { err }
                })
            } else {
                db.query(sql.admin.usersDelete, [userID], (err) => {
                    if (err) {
                        console.log(err);
                        res.send({
                            code: 505,
                            msg: "数据库操作失败!",
                            data: { err }
                        })
                    } else {
                        res.send({
                            code: 200,
                            msg: "数据库操作成功！",
                            data: {}
                        })
                    }
                })
            }
        })
    },
    //点击用户
    userDot: (req, res) => {
        let { userID } = req.query;
        db.query(sql.admin.userDot, [userID], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作失败！",
                    data: {}
                })
            } else {
                res.send({
                    code: 200,
                    msg: "数据库操作成功！",
                    data: { results }
                })
            }
        })
    },
    //编辑用户
    userRedact: (req, res) => {
        functionCtl.userRedact(sql.admin.userRedact, sql.admin.userRedact2, req, res);
    },
    //个人中心
    personalCenter: (req, res) => {
        let { userID } = req.query;
        db.query(sql.users.userAll, [userID], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作失败！",
                    data: {}
                })
            } else {
                res.send({
                    code: 200,
                    msg: "数据库操作成功！",
                    data: { results }
                })
            }
        })
    },
    //编辑个人中心
    personalCenterRedact: (req, res) => {
        functionCtl.userRedact(sql.users.userRedact, sql.users.userRedact2, req, res);
    },
    //删除歌单
    songListDelete: (req, res) => {
        let { songListName } = req.query;
        functionCtl.delete(sql.admin.songListsDelete, songListName, res);
    },
    //删除歌曲
    songDelete: (req, res) => {
        let { songsID } = req.query;
        db.query(sql.admin.songsDelete_list, songsID, (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作失败！",
                    data: { err }
                })
            } else {
                functionCtl.delete(sql.admin.songsDelete, songsID, res);
            }
        })
    },
    //编辑歌曲
    songRedact: (req, res) => {
        let { songsID, songName, word, singers, songWord, regionID, calssifyID, newSongID, recommendID } = req.body;
        let imgUrl = req.files[0].originalname;
        let url = req.files[1].originalname;
        db.query(sql.admin.songRedact, [singers], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作失败！",
                    data: {},
                })
            } else {
                let params = [songsID, songName, word, imgUrl, results, url, songWord, regionID, calssifyID, newSongID, recommendID];
                db.query(sql.admin.songRedact2, params, (err2, results2) => {
                    if (err2) {
                        res.send({
                            code: 505,
                            msg: "数据库操作失败！",
                            data: {},
                        })
                    } else {
                        res.send({
                            code: 505,
                            msg: "数据库操作成功！",
                            data: {},
                        })
                    }
                })
            }
        })
    },
    //添加歌曲
    // songAdd:(req,res)=>{
    //     let {songName,word,imgurl,singers,url,songWord,regionID,calssifID,newSongID,recommendID} = req.body;
    // },
    //编辑歌单
    songListRedact: (req, res) => {
        let { sid, uid, songListName } = req.body;
        let songListImg = req.file;
        db.query(sql.admin.songListRedact, [sid, uid, songListName, songListImg], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作失败！",
                    data: {},
                })
            } else {
                res.send({
                    code: 200,
                    msg: "数据库操作成功！",
                    data: {},
                })
            }
        })
    },
    //删除歌曲评论
    songCommentDelete: (req, res) => {
        let { comment_uid, word, time, song_sid } = req.query;
        functionCtl.deleteComments(comment_uid, word, time, song_sid, sql.admin.songCommentDelete, res);
    },
    //删除歌单评论
    songListCommentDelete: (req, res) => {
        let { comment_uid, word, time, songListName } = req.query;
        functionCtl.deleteComments(comment_uid, word, time, songListName, sql.admin.songListCommentDelete, res);
    },
}
